package com.iceland.common.monitor;
//监听数据变化

import lombok.extern.slf4j.Slf4j;

import java.util.LinkedList;
import java.util.List;

/**
 * 用于展示需要监听的数据信息
 */
@Slf4j
public class InfoMonitor {

    private static List<Info> infos=new LinkedList();
    private static Thread thread;
    public static void start(){
        thread=new Thread(new Runnable() {
            @Override
            public void run() {
                while (!Thread.interrupted()){
                    try {
                        log();
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();

    }

    public static void addInfo(Info info) {
        InfoMonitor.infos.add(info);
    }

    private static void log(){
        infos.forEach(info->{
            log.info(info.getMsg(),info.getObjects());
        });
    }

    public static void stop(){
        thread.interrupt();
    }

}
